package com.es.designmode.prototype;

/**
 * 原型模式
 * 用原型实例指定创建对象的种类，通过拷贝创建新的对象
 *
 * @author Will
 */
public class ClientSide {

    public static void main(String[] args) throws CloneNotSupportedException {

        /************************** 深克隆 ***************************/
        ConcretePrototype1 p1 = new ConcretePrototype1("001");
        TmpObj tmpObj = new TmpObj();
        tmpObj.setName("测试克隆数据01");
        p1.setTmpObj(tmpObj);
        ConcretePrototype1 cp1 = p1.clone();

        System.out.println(cp1.getTmpObj() == p1.getTmpObj());

        p1.setId("003");
        p1.setAge(13);

        tmpObj.setName("测试改变数据02");

        // 修改源对象后
        System.out.println("p1属性值：");
        System.out.println(p1.getId());
        System.out.println(p1.getTmpObj().getName());

        // 修改源对象后
        System.out.println("cp1属性值：");
        System.out.println(cp1.getId());
        System.out.println(cp1.getTmpObj().getName());

        /*********************  浅克隆  *************************/

        System.out.println("****************************");

        ConcretePrototype1 qp1 = new ConcretePrototype1("001");
        TmpObj qtmpObj = new TmpObj();
        qtmpObj.setName("测试浅克隆数据01");
        qp1.setTmpObj(qtmpObj);
        ConcretePrototype1 qcp1 = qp1.shallowClone();

        System.out.println(qcp1.getTmpObj() == qp1.getTmpObj());

        qp1.setId("003");
        qp1.setAge(13);

        qtmpObj.setName("测试改变数据02");

        // 修改源对象后
        System.out.println("qp1属性值：");
        System.out.println(qp1.getId());
        System.out.println(qp1.getTmpObj().getName());

        // 修改源对象后
        System.out.println("qcp1属性值：");
        System.out.println(qcp1.getId());
        System.out.println(qcp1.getTmpObj().getName());

    }
}
